<?php
namespace app\massage\controller;
use app\AdminRest;


use app\massage\model\CarAtvContent;
use app\massage\model\CarAtvList;
use app\massage\model\CarAtvRecord;
use app\massage\model\CarDriver;
use app\massage\model\CarGame;
use app\massage\model\User;
use think\App;


use think\facade\Db;


class AdminAtv extends AdminRest
{

    protected $model;

    protected $content_model;

    protected $record_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new CarAtvList();

        $this->content_model = new CarAtvContent();

        $this->record_model = new CarAtvRecord();

    }




    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:28
     * @功能说明:内容列表
     */
    public function contentList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        $data = $this->content_model->dataList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:28
     * @功能说明:内容列表
     */
    public function contentSelect(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $data = $this->content_model->where($dis)->order('id desc')->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:28
     * @功能说明:详情
     */
    public function contentInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->content_model->dataInfo($dis);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:29
     * @功能说明:添加活动详情
     */
    public function contentAdd(){

        $input = $this->_input;

        $input['uniacid']  = $this->_uniacid;

        $data = $this->content_model->dataAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:29
     * @功能说明:编辑活动详情
     */
    public function contentUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        if(isset($input['status'])&&in_array($input['status'],[-1,0])){

            $find = $this->model->atvContentIng($input['id']);

            if(!empty($find)){

                $this->errorMsg('该内容正在被使用');
            }

        }

        $data = $this->content_model->dataUpdate($dis,$input);

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:28
     * @功能说明:内容列表
     */
    public function atvList(){

        $input = $this->_param;

        $this->model->initAtv();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        if(!empty($input['status'])){

            $dis[] = ['atv_status','=',$input['status']];
        }

        $data = $this->model->dataList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:28
     * @功能说明:详情
     */
    public function atvInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:29
     * @功能说明:添加活动详情
     */
    public function atvAdd(){

        $input = $this->_input;

        $input['uniacid']  = $this->_uniacid;

        $data = $this->model->dataAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:29
     * @功能说明:编辑活动详情
     */
    public function atvUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $info = $this->model->dataInfo($dis);

        if(isset($input['atv_num'])&&$input['atv_num']<$info['have_num']){

            $this->errorMsg('报名人数不能小于已经报名人数,已报名'.$info['have_num']);
        }

        $data = $this->model->dataUpdate($dis,$input);

        if(!empty($input['atv_s_time'])&&!empty($input['atv_e_time'])){
            //同步活动报名时间
            $update = [

                'start_time' => $input['atv_s_time'],

                'end_time'   => $input['atv_e_time'],
            ];

            $this->record_model->dataUpdate(['atv_id'=>$input['id']],$update);
        }

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-09-16 15:41
     * @功能说明:用户报名列表
     */
    public function recordList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['status'])){

            $dis[] = ['b.atv_status','=',$input['status']];
        }

        if(!empty($input['pay_type'])){

            $dis[] = ['a.pay_type','=',$input['pay_type']];

        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $dis[] = ['a.pay_time','between',"{$input['start_time']},{$input['end_time']}"];
        }

        if(!empty($input['id'])){

            $dis[] = ['a.atv_id','=',$input['id']];
        }

        $data = $this->record_model->atvRecordList($dis,$input['limit'],[],$input['rank']);

        if(!empty($data['data'])){

            $user_model = new User();

            $driver_model = new CarDriver();

            $page = ($data['current_page']-1)*$data['per_page'];

            foreach ($data['data'] as $k=>&$v){

                $v['nickName']  = $user_model->where(['id'=>$v['user_id']])->value('nickName');

                $v['phone']     = $user_model->where(['id'=>$v['user_id']])->value('phone');

                $v['avatarUrl'] = $user_model->where(['id'=>$v['user_id']])->value('avatarUrl');

                $v['user_name'] = $driver_model->where(['user_id'=>$v['user_id']])->value('user_name');

//                $v['game_info']['top'] = $v['pay_type']==7?$page+$k+1:0;

                $v['game_info']['top'] = $this->record_model->getRecordTop($v);

            }

        }

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-09-16 15:46
     * @功能说明:报名详情
     */
    public function recordInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data['record_info'] = $this->record_model->dataInfo($dis);

        $data['record_info']['create_time'] = date('Y-m-d H:i:s',$data['record_info']['create_time']);

        $atv_model = new CarAtvList();

        $driver_model = new CarDriver();

        $data['atv_info'] = $atv_model->dataInfo(['id'=>$data['record_info']['atv_id']]);

        $user_model = new User();

        $data['record_info']['nickName']  = $user_model->where(['id'=>$data['record_info']['user_id']])->value('nickName');

        $data['record_info']['avatarUrl'] = $user_model->where(['id'=>$data['record_info']['user_id']])->value('avatarUrl');

        $data['record_info']['phone']     = $user_model->where(['id'=>$data['record_info']['user_id']])->value('phone');

        $data['record_info']['user_name'] = $driver_model->where(['user_id'=>$data['record_info']['user_id']])->value('user_name');

        $data['record_info']['game_info']['top'] = $this->record_model->getRecordTop($data['record_info']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:29
     * @功能说明:编辑
     */
    public function recordUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $data = $this->record_model->dataUpdate($dis,$input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-20 10:30
     * @功能说明:比赛情况列表
     */
    public function gameList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.have_game','=',1];

        if(!empty($input['name'])){

            $dis[] = ['b.nickName','like','%'.$input['name'].'%'];

        }

        if(!empty($input['major'])){

            $dis[] = ['a.major','=',$input['major']];
        }

        if(!empty($input['start_time'])){

            $dis[] = ['a.start_time','between',"{$input['start_time']},{$input['end_time']}"];
        }

        $game_model = new CarGame();

        $data = $game_model->topRecordList($dis,$input['limit']);


        return $this->success($data);

    }










}
